#include "rectangle.h"

namespace verema {

QGraphicsRectItem* Rectangle::at(int id)
{
    for(unsigned int counter=0 ; counter < id_vector.size(); ++counter)
    {
        if( id_vector.at(counter) == id) return(rect_vector.at(counter));
    }
    return(0);
}

int Rectangle::id(QGraphicsRectItem *gri)
{
    if(gri != 0)
    {
        for(int counter=0 ; counter < rect_vector.size(); ++counter)
        {
            if( rect_vector.at(counter) == gri) return(id_vector.at(counter));
        }
    }
    return(-1);
}

bool Rectangle::used(int id)
{
    for(unsigned int counter=0 ; counter < id_vector.size(); ++counter)
    {
        if( id_vector.at(counter) == id) return(used_vector.at(counter));
    }
    return(false);
}

void Rectangle::used(int id, bool state)
{
    for(unsigned int counter=0 ; counter < id_vector.size(); ++counter)
    {
        if( id_vector.at(counter) == id) used_vector.at(counter) = state;
    }
}

void Rectangle::id(QGraphicsRectItem *gri, int id)
{
    std::vector<int>::iterator it = id_vector.begin();
    for(QList<QGraphicsRectItem *>::Iterator it_rect = rect_vector.begin(); it_rect != rect_vector.end(); ++it_rect)
    {
        if( (*it_rect) == gri)
        {
            (*it) = id;
            break;
        }
        else
        {
            ++it;
        }
    }
}

void Rectangle::push_back(QGraphicsRectItem *gri, int id, bool used)
{
    rect_vector.push_back(gri);
    id_vector.push_back(id);
    used_vector.push_back(used);
}

QList<QGraphicsRectItem *>::Iterator Rectangle::begin()
{
    return(rect_vector.begin());
}

QList<QGraphicsRectItem *>::Iterator Rectangle::end()
{
    return(rect_vector.end());
}

void Rectangle::remove(int id)
{
    QList<QGraphicsRectItem *>::Iterator it_rect = rect_vector.begin();
    std::vector<bool>::iterator it_bool = used_vector.begin();

    for(std::vector<int>::iterator it = id_vector.begin(); it != id_vector.end(); ++it)
    {
        if( (*it) == id)
        {
            id_vector.erase(it);
            rect_vector.erase(it_rect);
            used_vector.erase(it_bool);
            break;
        }
        else
        {
            ++it_rect;
            ++it_bool;
        }
    }
}

void Rectangle::remove(QGraphicsRectItem *gri)
{
    std::vector<int>::iterator it = id_vector.begin();
    std::vector<bool>::iterator it_bool = used_vector.begin();

    for(QList<QGraphicsRectItem *>::Iterator it_rect = rect_vector.begin(); it_rect != rect_vector.end(); ++it_rect)
    {
        if( (*it_rect) == gri)
        {
            rect_vector.erase(it_rect);
            id_vector.erase(it);
            used_vector.erase(it_bool);
            break;
        }
        else
        {
            ++it;
            ++it_bool;
        }
    }
}

int Rectangle::count()
{
    return(id_vector.size());
}

void Rectangle::clear()
{
    rect_vector.clear();
    id_vector.clear();
    used_vector.clear();
}

}// namespace verema
